package com.xinjian.jianba.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.es.ChooseDto;
import com.xinjian.jianba.dto.es.EsServiceResult;
import com.xinjian.jianba.dto.es.FindDocDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.EsService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.service.YueJianService;
import com.xinjian.jianba.service.impl.EsServiceImpl;
import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.TimeUtil;

@RestController
@RequestMapping("/api/v3/jianba/Es")
public class YueJianControllor extends AbstractController{
	private static final Logger logger=LoggerFactory.getLogger(YueJianControllor.class);
	@Autowired
	private YueJianService yueJianService;
	@Autowired
	private ValidatorService validatorService;
	@Autowired
	private EsService esService;
	@PostMapping("/add")
	public String choose(@RequestBody ChooseDto chooseDto) throws IOException{
		logger.info("主题选择{}",JsonUtil.getJsonString(chooseDto));
		try {
			validatorService.valid(chooseDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		EsServiceResult result=yueJianService.indexDocumentUserId(chooseDto);
		String msg=null;
		if(result.getStatus()==EsServiceResult.ResultStatus.CREATED.getValue()){
			msg="CREATED";
		}else if(result.getStatus()==EsServiceResult.ResultStatus.UPDATED.getValue()){
			msg="UPDATED";
		}else{
			msg="操作失败";
		}
		return this.responseClientJsonData(ResponseCode.SUCCESS, msg);
	}
	@PostMapping("/update")
	public String upserts(@RequestBody ChooseDto chooseDto) throws IOException{
		logger.info("主题选择{}",JsonUtil.getJsonString(chooseDto));
		try {
			validatorService.valid(chooseDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		EsServiceResult result=yueJianService.updateDocumentsUserId(chooseDto);
		String msg=null;
		if(result.getStatus()==EsServiceResult.ResultStatus.CREATED.getValue()){
			msg="CREATED";
		}else if(result.getStatus()==EsServiceResult.ResultStatus.UPDATED.getValue()){
			msg="UPDATED";
		}else{
			msg="操作失败";
		}
		return this.responseClientJsonData(ResponseCode.SUCCESS, msg);
	}
	@PostMapping("/findByUid")
	public String findDoc(@RequestBody Map<String,String> map) throws IOException{
		logger.info("是否在约见池/findByUid{}",JsonUtil.getJsonString(map));
		String userId=map.get("userId");
		if(userId==null||userId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "用户id不能为空");
		}
		EsServiceResult result=yueJianService.findByUid(userId);
		if(result.getStatus()==EsServiceResult.ResultStatus.SUCCESS.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,result.getMap());
		}else if(result.getStatus()==EsServiceResult.ResultStatus.NOT_FOUND.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, "NOT_FOUND");
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "请求失败");
		}
	}
	@PostMapping("/start/meet")
	public String findPersons(@RequestBody FindDocDto findDocDto) throws IOException{
		logger.info("开始约见/start/meet{}",JsonUtil.getJsonString(findDocDto));
		try {
			validatorService.valid(findDocDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		EsServiceResult result=yueJianService.findPersons(findDocDto);
		if(result.getStatus()==EsServiceResult.ResultStatus.SUCCESS.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,result.getList());
		}else if(result.getStatus()==EsServiceResult.ResultStatus.NOT_FOUND.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, "NOT_FOUND");
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "请求失败");
		}
	}
	@PostMapping("/del")
	public String del(@RequestBody Map<String,String> map){
		String userId=map.get("userId");
		if(userId==null||userId.equals("")){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "用户id不能为空");
		}
		EsServiceResult result=yueJianService.deleteDocByDocId(userId);
		if(result.getStatus()==EsServiceResult.ResultStatus.DELETED.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else if(result.getStatus()==EsServiceResult.ResultStatus.NOT_FOUND.getValue()){
			return this.responseClientJsonData(ResponseCode.SUCCESS, "NOT_FOUND");
		}else if(result.getStatus()==EsServiceResult.ResultStatus.CONFLICT.getValue()){
			return this.responseClientJsonData(ResponseCode.ERROR, "CONFLICT");
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "操作失败");
		}
	}
	
}
